package com.zy.explore.rocketmq.interfaces.facade.example;

import com.zy.explore.rocketmq.interfaces.frm.CommonIdFrm;
import com.zy.explore.rocketmq.interfaces.frm.CommonIdsFrm;
import com.zy.explore.rocketmq.interfaces.vo.ResultVO;
import com.zy.explore.rocketmq.application.service.RedisService;
import com.zy.explore.rocketmq.interfaces.frm.ExampleSaveFrm;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * description Redis示例Facade
 *
 * @author ZY
 * @date 2021年05月20日 23:09
 */
@Api(tags = "Redis示例Facade")
@RestController
@RequestMapping("/redis")
public class RedisExampleFacade {

    /**
     * 注入服务
     */
    @Resource
    private RedisService service;

    /**
     * 新增数据
     *
     * @param frm 待新增的Frm对象
     * @return 成功返回新增后的VO对象，失败返回null
     */
    @ApiOperationSupport(order = 10, author = "🇨🇳中国印🇨🇳", ignoreParameters = {"id"})
    @ApiOperation("①新增数据")
    @PostMapping("/insert")
    public ResultVO<String> insert(@RequestBody @Validated ExampleSaveFrm frm) {
        String uuid = UUID.randomUUID().toString().replace("-", "");
        frm.setId(uuid);
        service.set(uuid, frm);
        return ResultVO.getSuccess(uuid);
    }

    /**
     * 删除数据
     *
     * @param frm 主键Frm对象
     * @return 1:删除成功 其他:删除失败
     */
    @ApiOperationSupport(order = 20, author = "🇨🇳中国印🇨🇳")
    @ApiOperation("②删除数据")
    @PostMapping("/delete")
    public Integer delete(@RequestBody @Validated CommonIdFrm frm) {
        service.del(frm.getId());
        return 1;
    }


    /**
     * 通过主键查询单条数据
     *
     * @param frm 主键Frm对象
     * @return 成功返回对应ID的对象，查询不到返回null
     */
    @ApiOperationSupport(order = 30, author = "🇨🇳中国印🇨🇳")
    @ApiOperation("③根据主键查询数据")
    @PostMapping("/findById")
    public ResultVO<Object> findById(@RequestBody @Validated CommonIdFrm frm) {
        return ResultVO.getSuccess(service.get(frm.getId()));
    }

    /**
    * 通过主键集合查询多条数据
    *
    * @param frm 主键Frm对象
    * @return 成功返回对应ID集合的对象集合，查询不到返回null
    */
    @ApiOperationSupport(order = 40, author = "🇨🇳中国印🇨🇳")
    @ApiOperation("④根据主键集批量查询数据")
    @PostMapping("/findByIds")
    public List<Object> findById(@RequestBody @Validated CommonIdsFrm frm) {
        return service.mGet(frm.getIds());
    }

}
